home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / ungetc.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  2KB  |  104 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: ungetc.c,v 1.3 1996/08/13 13:52:52 digulla Exp $
  4.     $Log: ungetc.c,v $
  5.     Revision 1.3  1996/08/13 13:52:52  digulla
  6.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  7.     Replaced __AROS_LA by __AROS_LHA
  8.  
  9.     Revision 1.2  1996/08/01 17:40:58  digulla
  10.     Added standard header for all files
  11.  
  12.     Desc:
  13.     Lang: english
  14. */
  15. #include <clib/exec_protos.h>
  16. #include "dos_intern.h"
  17.  
  18. #ifndef EOF
  19. #define EOF -1
  20. #endif
  21.  
  22. /*****************************************************************************
  23.  
  24.     NAME */
  25.     #include <clib/dos_protos.h>
  26.  
  27.     __AROS_LH2(LONG, UnGetC,
  28.  
  29. /*  SYNOPSIS */
  30.     __AROS_LHA(BPTR, file,      D1),
  31.     __AROS_LHA(LONG, character, D2),
  32.  
  33. /*  LOCATION */
  34.  
  35.     struct DosLibrary *, DOSBase, 53, Dos)
  36.  
  37. /*  FUNCTION
  38.     Push a character back into a read filehandle. If you've read
  39.     a character from that file you may always push at least 1 character
  40.     back. UnGetC(file,-1) ungets the last character read. This also
  41.     works for EOF.
  42.  
  43.     INPUTS
  44.     file      - Filehandle you've read from.
  45.     character - Character to push back or EOF.
  46.  
  47.     RESULT
  48.     !=0 if all went well, 0 if the character couldn't be pushed back.
  49.     IoErr() gives additional information in that case.
  50.  
  51.     NOTES
  52.  
  53.     EXAMPLE
  54.  
  55.     BUGS
  56.  
  57.     SEE ALSO
  58.     FGetC(), IoErr()
  59.  
  60.     INTERNALS
  61.  
  62.     HISTORY
  63.     29-10-95    digulla automatically created from
  64.                 dos_lib.fd and clib/dos_protos.h
  65.  
  66. *****************************************************************************/
  67. {
  68.     __AROS_FUNC_INIT
  69.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  70.  
  71.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  72.  
  73.     /* Get pointer to filehandle */
  74.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  75.  
  76.     /* If the file is in write mode there was nothing read recently */
  77.     if(fh->fh_Flags&FHF_WRITE)
  78.     {
  79.     *result=ERROR_SEEK_ERROR;
  80.     return 0;
  81.     }
  82.  
  83.     /* Unget EOF character if the last character read was an EOF */
  84.     if(character==EOF&&fh->fh_End==fh->fh_Buf)
  85.     {
  86.     fh->fh_Pos++;
  87.     return EOF;
  88.     }
  89.  
  90.     /* Test if I may unget a character on this file */
  91.     if(fh->fh_Pos==fh->fh_Buf)
  92.     {
  93.     *result=ERROR_SEEK_ERROR;
  94.     return 0;
  95.     }
  96.  
  97.     /* OK. Unget character and return. */
  98.     fh->fh_Pos--;
  99.     if(character!=EOF)
  100.     *fh->fh_Pos=character;
  101.     return character?character:1;
  102.     __AROS_FUNC_EXIT
  103. } /* UnGetC */
  104.